package com.dm.cloud.tkmybatis.service;

import cn.hutool.core.util.StrUtil;
import com.dm.cloud.core.enums.OrderSort;
import lombok.Data;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 用于自定义排序
 */
public class OrderAlgorithm {

    @Data
    public static class OrderField implements Comparable<OrderField>{
        /**
         * 字段名称，不填则用字段名称驼峰转下划线
         * @return
         */
        String fieldName;
        /**
         * 排序方式，默认倒排
         * @return
         */
        OrderSort sort;

        /**
         * 序号
         *
         */
        int index;

        /**
         * 去当前字段的排序语句
         * @return
         */
        public String sortStr(){
            return StrUtil.toUnderlineCase(fieldName)+" "+sort.name();
        }

        @Override
        public int compareTo(OrderField o) {
            return 0;
        }
    }

    List<OrderField> orderFiledList=new ArrayList<>();

    /**
     * 添加到缓存中
     * @param orderField
     */
    public void append(OrderField orderField){

        orderFiledList.add(orderField);
    }

    /**
     * 获取全部排序语句
     * @return
     */
    public String getOrderStr(){
        Collections.sort(orderFiledList);
        List<String> reList=new ArrayList<>();
        for (OrderField orderField : orderFiledList) {
            reList.add(orderField.sortStr());
        }
        return String.join(",",reList);
    }

}
